<template>
  <div class="app-container">
    <!-- 顶部按钮区域开始 -->
    <el-form :model="queryParams" ref="queryForm" inline label-width="68px">
      <el-form-item label="订单号">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单号查询"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="queryParams.status" placeholder="请选择" @change="getList">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableList" border :height="tableHeight">
      <el-table-column label="序号" width="55">
        <template slot-scope="scope">
          <div style="text-align:center;">
            {{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderId" width="240px">
        <template slot-scope="scope">
          <el-popover placement="top" width="240px" trigger="hover" :content="scope.row.orderId">
            <div slot="reference" style="width:220px;overflow: hidden;text-overflow: ellipsis;white-space:nowrap;">{{scope.row.orderId}}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="下单用户" align="center" prop="attributionName" show-overflow-tooltip width="140px" />
      <el-table-column label="商品名称" align="center" prop="commodity" show-overflow-tooltip min-width="280px" />
      <el-table-column label="订单状态" align="center" width="120px">
        <template slot-scope="scope">
          {{scope.row.status | filterStatus}}
        </template>
      </el-table-column>
      <el-table-column label="订单总额" align="center" width="120px">
        <template slot-scope="scope">
          {{scope.row.totalAmount}}元
        </template>
      </el-table-column>
      <el-table-column label="订单创建时间" align="center" prop="createTime" show-overflow-tooltip width="180px" />
      <el-table-column label="操作" align="center" width="80px">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editGoods(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 修改订单对话框 -->
    <el-dialog title="修改订单状态" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="订单状态" prop="totalAmount">
          <el-select v-model="form.status" placeholder="请选择" style="width:100%;">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { sellerList, editSellerList } from '@/api/goodsAdmin'
export default {
  data() {
    return {
      tableHeight: 200,
      options: [
        {
          value: 1,
          label: '待处理'
        }, {
          value: 2,
          label: '已发货'
        }, {
          value: 3,
          label: '已收货'
        }, {
          value: 4,
          label: '待付款'
        }, {
          value: 5,
          label: '已取消'
        }
      ], //订单状态
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        orderId: null,
        status: null,
        pageNum: 1,
        pageSize: 50
      },
      tableList: [],
      loading: false,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {}
    }
  },

  created() {
    this.getList()
  },

  mounted() {
    this.$nextTick(function () {
      this.tableHeight = window.screen.height - 400
      // 监听窗口大小变化
      let self = this
      window.onresize = function () {
        self.tableHeight = window.screen.height - 400
      }
    })
  },

  filters: {
    filterStatus(val) {
      switch (val + '') {
        case '1':
          return '待处理'
        case '2':
          return '已发货 '
        case '3':
          return '已收货'
        case '4':
          return '待付款'
        case '5':
          return '已取消'
      }
    }
  },
  methods: {
    editGoods(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    /** 提交按钮 */
    submitForm() {
      editSellerList(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    getList() {
      this.loading = true
      sellerList(this.queryParams).then(res => {
        this.loading = false
        this.total = res.total
        this.tableList = res.rows
      })
    },

    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    resetQuery() {
      this.queryParams.status = null
      this.queryParams.orderId = null
      this.queryParams.pageNum = 1
      this.getList()
    }
  }
}
</script>